﻿using Daemon.Data.Substructure.Enums;
using Daemon.Middleware;
using Daemon.Repository.Contract;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DaemonAPI.SchdulerJob
{
	public class UserOrderJob : IJob
	{
		private readonly ITakeAwayUserOrderRepository _takeAwayUserOrderRepository;

		public UserOrderJob()
		{
			_takeAwayUserOrderRepository = ServiceLocator.Resolve<ITakeAwayUserOrderRepository>();
		}

		public async Task Execute(IJobExecutionContext context)
		{
			await CancelUserOrder();
		}

		private async Task CancelUserOrder()
		{
			var uselessUserOrders = _takeAwayUserOrderRepository.FindAll().Where(r => r.Status == (int)UserOrderStatusEnum.UnPay && r.IsPredetermine == null && r.AddTime < DateTime.Now.AddMinutes(-30));
			var num = _takeAwayUserOrderRepository.DeleteRange(uselessUserOrders);
			await Task.CompletedTask;
		}
	}
}
